package com.ling.pl.security.service;

import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.BatchUtils;
import com.ling.pl.core.commons.utils.reflect.ReflectUtils;
import com.ling.pl.core.dao.IDao;
import com.ling.pl.core.service.impl.BaseService;
import com.ling.pl.security.dao.DefaultUserDao;
import com.ling.pl.security.model.DefaultUser;
import com.ling.pl.security.model.DefaultUserVO;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author bo.wang
 */
public class BaseDefaultUserService extends BaseService {

	public static final String BEAN_ID = "defaultUserService";

	@Resource
	DefaultUserDao defaultUserDao;

	public IDao getDao() {
		return defaultUserDao;
	}

	public List<DefaultUserVO> convertDefaultUserToVO(List<DefaultUser> models) {
		List<DefaultUserVO> result = new ArrayList<DefaultUserVO>();
		for (DefaultUser initiation : models) {
			DefaultUserVO inparam = convertDefaultUserToVO(initiation);
			result.add(inparam);
		}
		return result;
	}

	public DefaultUserVO convertDefaultUserToVO(DefaultUser model) {
		DefaultUserVO inparam = new DefaultUserVO();
		ReflectUtils.copyProperties(model, inparam);
		return inparam;
	}

	public DefaultUser convertDefaultUserVOTo(DefaultUserVO vo) {
		DefaultUser entity = new DefaultUser();
		ReflectUtils.copyProperties(vo, entity);
		return entity;
	}

	public List<DefaultUser> convertDefaultUserVOTo(List<DefaultUserVO> vos) {
		List<DefaultUser> result = new ArrayList<DefaultUser>();
		for (DefaultUserVO vo : vos) {
			DefaultUser entity = convertDefaultUserVOTo(vo);
			result.add(entity);
		}
		return result;
	}

	public DaoPage findDefaultUserVOByParams(Map params, Integer pageIndex,
											 Integer pageSize) {
		if (params == null) {
			params = new HashMap();
		}
		DaoPage daoPage = defaultUserDao.findDefaultUserByParams(params, pageIndex, pageSize);
		daoPage.setResult(convertDefaultUserToVO((List<DefaultUser>) daoPage.getResult()));
		return daoPage;
	}

	public List<DefaultUserVO> findDefaultUserVOByParams(Map params) {
		if (params == null) {
			params = new HashMap();
		}
		List<DefaultUser> temp = defaultUserDao.findDefaultUserByParams(params);
		return convertDefaultUserToVO(temp);
	}

    public DaoPage findDefaultUserByParams(Map params, Integer pageIndex,
										   Integer pageSize) {
		if (params == null) {
			params = new HashMap();
        }
		DaoPage daoPage = defaultUserDao.findDefaultUserByParams(params, pageIndex, pageSize);
        return daoPage;
    }

    public List<DefaultUser> findDefaultUserByParams(Map params) {
		if (params == null) {
			params = new HashMap();
        }
		List<DefaultUser> temp = defaultUserDao.findDefaultUserByParams(params);
        return temp;
    }


	public void saveDefaultUserDataListsMap(Map<String, List<DefaultUserVO>> dataListsMap) {
		Collection<DefaultUserVO> deleteList = BatchUtils.to(DefaultUserVO.class).getDeleteEntities(dataListsMap);
		Collection<DefaultUserVO> insertList = BatchUtils.to(DefaultUserVO.class).getInsertEntities(dataListsMap);
		Collection<DefaultUserVO> updateList = BatchUtils.to(DefaultUserVO.class).getModifiedEntities(dataListsMap);
		if (updateList != null && updateList.size() > 0) {
			for (DefaultUserVO vo : updateList) {
				DefaultUser entity = convertDefaultUserVOTo(vo);
				defaultUserDao.update(entity);
			}
//			defaultUserDao.updateAll(updateList);
		}
		if (insertList != null && insertList.size() > 0) {
			for (DefaultUserVO vo : insertList) {
				DefaultUser entity = convertDefaultUserVOTo(vo);
				defaultUserDao.save(entity);
				vo.setId(entity.getId());
			}
//			defaultUserDao.saveAll(insertList);
		}
		if (deleteList != null && deleteList.size() > 0) {
			for (DefaultUserVO vo : deleteList) {
				DefaultUser entity = new DefaultUser();
				entity.setId(vo.getId());
				defaultUserDao.remove(entity);
			}
//			defaultUserDao.removeAll(deleteList);
		}

	}
}

